﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
using System.Collections;
using System.Threading;

namespace LogExport.UI
{
    public partial class DbSettings : Form
    {
        public DbSettings()
        {
            InitializeComponent();
#if DEBUG
            this.comboBox1.Text = "MS SQLServer";
            this.textBox1.Text = ".";
            this.textBox2.Text = "sa";
            this.textBox3.Text = "123";
            //this.textBox4.Text = "tempdb";
            this.textBox5.Text = "iislog";
#endif
            comboBox2.Click += new EventHandler(comboBox2_Click);
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            //worker.RunWorkerAsync();
        }

        void comboBox2_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConn = new SqlConnection();
            sqlConn.ConnectionString = string.Format("Data Source={0};User ID={1};Password={2};connect timeout=2",
                this.DBAddr,
                this.DBUser,
                this.DBPass);
            try
            {
                sqlConn.Open();
                var cmd = sqlConn.CreateCommand();
                cmd.CommandText = "SELECT Name FROM Master..SysDatabases ORDER BY Name";
                 var r = cmd.ExecuteReader();
                var  names = new List<string>();
                while(r.Read())
                {
                    names.Add(r.GetString(0));
                }
                r.Close();
                this.comboBox2.Items.Clear();
                this.comboBox2.Items.AddRange(names.ToArray());
                sqlConn.Close();
            }
            catch (SqlException ex)
            {
                this.comboBox2.Items.Clear();
                MessageBox.Show(ex.Message);
            }
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {

        }

        Queue<Host> events = new Queue<Host>();
        BackgroundWorker worker = new BackgroundWorker();

        private void button1_Click(object sender, EventArgs e)
        {
            if (DBType == DBType.MySql)
            {
                MySqlConnection mysqlConn = new MySqlConnection();
                mysqlConn.ConnectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
                    this.DBAddr,
                    this.DBName,
                    this.DBUser,
                    this.DBPass);
                try
                {
                    mysqlConn.Open();
                    mysqlConn.Close();
                    DialogResult = DialogResult.OK;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if (DBType == DBType.MSSql)
            {
                SqlConnection sqlConn = new SqlConnection();
                sqlConn.ConnectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",
                    this.DBAddr,
                    this.DBName,
                    this.DBUser,
                    this.DBPass);
                try
                {
                    sqlConn.Open();
                    sqlConn.Close();
                    DialogResult = DialogResult.OK;
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            if (DialogResult == DialogResult.OK)
            {
                Close();
            }
        }

        public DBType DBType
        {
            get
            {
                switch (comboBox1.Text)
                {
                    case "MS SQLServer":
                        return DBType.MSSql;
                    case "MySql":
                        return DBType.MySql;
                }
                return DBType.None;
            }
        }
        public String DBAddr
        {
            get { return textBox1.Text; }
        }
        public String DBUser
        {
            get { return textBox2.Text; }
        }
        public String DBPass
        {
            get { return textBox3.Text; }
        }
        public String DBName
        {
            get
            {
                if (comboBox2.SelectedItem != null)
                    return comboBox2.SelectedItem.ToString();
                return string.Empty;
            }
        }
        public String DBTNam
        {
            get { return textBox5.Text; }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.Text)
            {
                case "MS SQLServer":
                    textBox6.Text = "1433";
                    EnableControls(sender, e);
                    break;
                case "MySql":
                    textBox6.Text = "3360";
                    EnableControls(sender, e);
                    break;
            }
        }

        private void EnableControls(object sender, EventArgs e)
        {
            this.textBox1.Enabled = true;
            this.textBox2.Enabled = true;
            this.textBox3.Enabled = true;            
            this.textBox5.Enabled = true;
            this.textBox6.Enabled = true;
            //this.comboBox1.Enabled = true;
            this.comboBox2.Enabled = true;
            this.checkBox1.Enabled = true;
            if (!this.checkBox1.Checked)
            {
                this.textBox6.Enabled = false;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            textBox6.Enabled = checkBox1.Checked;
            if (!checkBox1.Checked)
            {
                switch (comboBox1.Text)
                {
                    case "MS SQLServer":
                        textBox6.Text = "1433";
                        break;
                    case "MySql":
                        textBox6.Text = "3360";
                        break;
                    default:
                        textBox6.Text = "";
                        break;
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBox1.Text.Trim()) &&
               !string.IsNullOrEmpty(this.textBox6.Text.Trim()) &&
               !string.IsNullOrEmpty(this.textBox2.Text.Trim()) &&
               !string.IsNullOrEmpty(this.textBox3.Text.Trim()))
            {
                var h = new Host();
                h.Ip = this.textBox1.Text;
                h.port = this.textBox6.Text;
                h.pwd = this.textBox3.Text;
                h.uid = this.textBox2.Text;
                events.Enqueue(h);
            }
        }
    }
    public enum DBType
    {
        None = 0,
        MySql = 1,
        MSSql = 2
    }
    public class Host
    {
        public string Ip { get; set; }
        public string uid { get; set; }
        public string pwd { get; set; }
        public string port { get; set; }
    }

}